package com.iscc.fake.dao.Impl;

import com.iscc.fake.dao.UserDAO;
import com.iscc.fake.domain.User;
import com.iscc.fake.util.DataSourceUtile;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl implements UserDAO {
    @Override
    public int addUser(User user) {
        String sql ="INSERT  INTO user (account,password,power) values (?,?,?)";
        String a="1";
        //获取Connection对象
        int result=-1;
        Connection conn= null;
        PreparedStatement ps=null;
        try {
        conn = DataSourceUtile.getConnection();
        //预处理对象
        ps =conn.prepareStatement(sql);
        //
        ps.setString(1,user.getAccount());
        ps.setString(2,user.getPassword());
        ps.setString(3,a);
        //执行SQL语句，查询-executeQuery，增删改-executeUpdate():返回int:影响行数。
        result=ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
                DataSourceUtile.closeReource(ps, conn);
            }
        return result;
    }

    @Override
    public User login(String account) {
        User user =null;
        String sql ="Select * From user where BINARY account =?";
        Connection conn =null;
        PreparedStatement ps =null;
        ResultSet rs =null;
        try {
            conn = DataSourceUtile.getConnection();
            ps =conn.prepareStatement(sql);
            ps.setString(1,account);
            rs =ps.executeQuery();
            if(rs.next()){
                user = new User();
                user.setUno(rs.getInt(1));
                user.setAccount(rs.getString(2));
                user.setPassword(rs.getString(3));
                user.setSex(rs.getString(4));
                user.setBirthday(rs.getString(5));
                user.setEmail(rs.getString(6));
                user.setPhone(rs.getString(7));
                user.setUname(rs.getString(8));
                user.setStatus(rs.getString(9));
                user.setPower(rs.getString(10));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DataSourceUtile.closeReource(rs,ps,conn);
        }
        return user;
    }
    @Override
    public User gerenzhongxin(int uno) {
        String sql = "select uno,gender,birthday,email,phone,uname,status,power,password,realname,account from user where uno = ?";

        //获取Connection对象
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        User user = null;

        try {
            // 获得连接池对象
            conn = DataSourceUtile.getConnection();
            // 预处理对象
            ps = conn.prepareStatement(sql);
            // 预编译
            ps.setInt(1,uno);
            //获得结果集
            rs = ps.executeQuery();

            // 获得user对象.
            user = new User();
            while (rs.next()){
                user.setUno(rs.getInt(1));
                user.setSex(rs.getString(2));
                user.setBirthday(rs.getString(3));
                user.setEmail(rs.getString(4));
                user.setPhone(rs.getString(5));
                user.setUname(rs.getString(6));
                user.setStatus(rs.getString(7));
                user.setPower(rs.getString(8));
                user.setPassword(rs.getString(9));
                user.setRealname(rs.getString(10));
                user.setAccount(rs.getString(11));
            }



        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            DataSourceUtile.closeReource(rs,ps,conn);
        }


        return user;
    }


    /***
     *
     * @param user user，包含所有的信息
     * @return 返回受影响的行数，判断是否修改成功
     */
    @Override
    public int updateInfo(User user) {
        String sql = "UPDATE USER SET uname=?, gender=?, birthday=?, email=?, phone=?,realname=?, STATUS=?, POWER=? WHERE uno=?";


        // 获得connection 对象
        Connection conn = null;
        PreparedStatement ps = null;
        int rs = -1;

        // 获得连接池对象
        try {
            conn = DataSourceUtile.getConnection();
            // 预处理对象
            ps = conn.prepareStatement(sql);
            // 预编译
            ps.setString(1,user.getUname());
            ps.setString(2,user.getSex());
            ps.setString(3,user.getBirthday());
            ps.setString(4,user.getEmail());
            ps.setString(5,user.getPhone());
            ps.setString(6,user.getRealname());
            ps.setString(7,user.getStatus());
            ps.setString(8,user.getPower());
            ps.setInt(9,user.getUno());



            rs = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            DataSourceUtile.closeReource(ps,conn);
        }

        return rs;
    }
    // 修改密码 通过uno修改密码
    @Override
    public int updatePwd(String password,int uno){
        String sql = "UPDATE USER SET PASSWORD=? WHERE uno=?";
        int rs = -1;
        // 获得对象
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = DataSourceUtile.getConnection();
            ps = conn.prepareStatement(sql);
            // 预编译
            ps.setString(1,password);
            ps.setInt(2,uno);
            // 执行sql语句 并获得受影响行数
            rs = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            DataSourceUtile.closeReource(ps,conn);
        }
        return rs;
    }
}
